package com.sgb.kjwl.viewmodel.manage

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.swgk.core.base.api.ICallBack
import com.swgk.core.util.PickSelectEntity
import com.sgb.kjwl.model.entity.manage.UserInquiryEntity
import com.swgk.core.base.model.entity.ListResponse
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity
import com.sgb.kjwl.view.ui.activity.manage.WebSiteInquiryActivity
import com.sgb.kjwl.view.ui.activity.manage.WebSitePostInquiryActivity
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.H5ReEntity
import com.swgk.core.dialog.DialogHelper
import com.swgk.core.util.MToast
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.io.File

class WebSiteInquiryModel  : BaseViewModel {
    private var mActivity : WebSiteInquiryActivity? = null

    constructor(activity : WebSiteInquiryActivity){
        this.mActivity = activity
    }

    //页数
    private var page = 1
    private val limit = "10" //每页条数
    private var currentCount = 10 //每页条数
    private var pages = 1 //总页数


    var inquiryTypes = MutableLiveData(ArrayList<PickSelectEntity<*>>())

    var mPostInquiryList = MutableLiveData(ArrayList<UserInquiryEntity>())

    var mReceiverInquiryList = MutableLiveData(ArrayList<UserInquiryEntity>())

    var mFileInfoEntity = MutableLiveData(null)

    constructor(){

    }


    //上传多图片
    fun uploadMutiFileModel(activity : WebSitePostInquiryActivity, file: File?, type: Int, completeNum: Int) {
        val photos: MutableMap<String, RequestBody> = HashMap()
        val mediaType = "multipart/form-data".toMediaTypeOrNull()
        val requestBody = RequestBody.create(mediaType, file!!)
        photos["file" + "\"; filename=\"" + completeNum + "image.jpg\""] = requestBody
        DialogHelper.showProgressDialog(activity, null, "图片上传中" + completeNum + "/" + activity.imgTotal + "...", 0, false, null).setCanceledOnTouchOutside(false)
        APIManager.getInstance().uploadAPI.uploadFile(photos).enqueue(object : ICallBack<BaseEntity<*>>(){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data == null || data.data == null){
                    MToast.showToast(activity, "上传失败")
                    return
                }
                activity.uploadRefresh(data.data.toString(), file.name, type)
            }

        })
    }


    //获取询函类型
    fun getAdminInquiryTypes(context: Context?){
        DialogHelper.showProgressDialog(context!!, null, "数据加载中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().websiteInfoAPI.adminInquiry.enqueue(object :
            ICallBack<BaseEntity<List<PickSelectEntity<*>>>>(){
            override fun onSuccess(data: BaseEntity<List<PickSelectEntity<*>>>?) {
                if (data != null && data.data != null ) {
                    inquiryTypes.postValue(data.data as ArrayList<PickSelectEntity<*>>)
                }
            }

        })
    }

    //函询 我发起的列表
    fun getMyPostInquiryList(context: Context? , inquiryType : String, isAnswer: Int?, isLoadMore : Boolean){
        if (isLoadMore) {
            if (currentCount < 10) {
                return
            }
        }
        val map = HashMap<String, Any?>()
        map.put("enquiryType", inquiryType)
        map.put("isAnswer", isAnswer ?: "")
        page = if (isLoadMore) page + 1 else 1
        if (isLoadMore){
           if (pages < page){
               return
           }
        }
        map.put("page", page)
        map.put("limit", limit)
        DialogHelper.showProgressDialog(context!!, null, "数据加载中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().websiteInfoAPI.getMyPostInquiryList(map).enqueue(object : ICallBack<BaseEntity<ListResponse<List<UserInquiryEntity>>>>(){
            override fun onSuccess(data: BaseEntity<ListResponse<List<UserInquiryEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null){
                    currentCount = data.data.list.size
                    pages = data.data.pageSize
                    mPostInquiryList.postValue(data.data.list as ArrayList<UserInquiryEntity>)
                }
            }
        })
    }

    //函询 我收到的列表
    fun getMyReceiverInquiryList(context: Context? ,inquiryType : String, isAnswer: Int?, isLoadMore : Boolean){
        if (isLoadMore) {
            if (currentCount < 10) {
                return
            }
        }
        val map = HashMap<String, Any?>()
        map.put("enquiryType", inquiryType)
        map.put("isAnswer", isAnswer ?: "")
        page = if (isLoadMore) page + 1 else 1
        if (isLoadMore){
            if (pages < page){
                return
            }
        }
        map.put("page", page)
        map.put("limit", limit)
        DialogHelper.showProgressDialog(context!!, null, "数据加载中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().websiteInfoAPI.getMyReceiverInquiryList(map).enqueue(object : ICallBack<BaseEntity<ListResponse<List<UserInquiryEntity>>>>(){
            override fun onSuccess(data: BaseEntity<ListResponse<List<UserInquiryEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null){
                    currentCount = data.data.list.size
                    pages = data.data.pageSize
                    mReceiverInquiryList.postValue(data.data.list as ArrayList<UserInquiryEntity>)
                }
            }

        })

    }

    //发起函询
    fun postInquiry(context: Context, entity: UserInquiryEntity) {
        DialogHelper.showProgressDialog(context, null, "提交中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().websiteInfoAPI.postInquiry(entity).enqueue(object : ICallBack<BaseEntity<*>>(){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null){
                    message.postValue("提交成功")
                }
            }

        })
    }

    //跳转h5详情
   fun getH5Url(mContext: Context, no: String, param: String, flagNo : String, requestCode : Int) {
        DialogHelper.showProgressDialog(mContext, null, "数据加载中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().pickAPI.getUrlSetting(param)
            .enqueue(object : ICallBack<BaseEntity<H5ReEntity?>?>() {
                override fun onSuccess(data: BaseEntity<H5ReEntity?>?) {
                    if (data == null || data.state == null) {
                        return
                    }
                    if (data.state == APIManager.RESPONSE_OK) {
                        var url = data.data!!.url
                        url = "$url?no=$no&flagNo=$flagNo"
                        WebViewX5HostActivity.start(mContext, url, requestCode)
                    } else {
                        MToast.showToast(mContext, data.msg)
                    }
                }
            })
    }
}